<?php

/*
 *  根据登录用户的角色得到该用户的访问授权菜单
 */



$BASE_URL = Yii::$app->request->hostInfo . Yii::$app->homeUrl . "?r=";

//不同角色用户得到不同的菜单项
$access = app\models\Menu::getMenusByRole();
//得到该用户有权访问的菜单id
$accessStr = $access['0']['access'];
$accessArr = explode(',', $accessStr);





for ($i = 0; $i < count($accessArr); $i++) {
    //查出menu表中所有一级菜单
    $menu = mdm\admin\models\Menu::find()->select('id,name')->where(['and', 'id=' . $accessArr[$i], 'parent is NULL'])->all();

    if ($menu != null) {
        $arrMenu['id'] = $menu[0]->id;
        $arrMenu['name'] = $menu[0]->name;
        $id = $arrMenu['id'];
        //查出一级菜单下的子菜单
        $items = mdm\admin\models\Menu::find()->select('id,name,route')->where(['parent' => $id])->all();
        if ($items != null) {
            $ie = [];
            foreach ($items as $item) {
                $idStr = $item->id."";
                //根据用户的授权访问菜单id，判断用户是否可以访问该一级菜单下的所有子菜单
                if (strpos($accessStr, $idStr)) {
                    $it['id'] = $item->id;
                    $it['name'] = $item->name;
                    $it['route'] = $item->route;
                    $ie[] = $it;
                }
            }
            $arrMenu['items'] = $ie;
        } else {
            $arrMenu['items'] = array();
        }
        $arr[] = $arrMenu;
    }
}

echo "<script type='text/javascript'> var func_array={};";


for ($j = 0; $j < count($arr); $j++) {
    $mid = $arr[$j]['id'];
    $mname = $arr[$j]['name'];
    //输出一级菜单项
    //格式：func_array['m46']=['OA办公'];
    
    echo "func_array['m" . $mid . "']=['" . $mname . "'];";

    $a = $arr[$j]['items'];
    //判断是否有二级菜单
    if ($a != null) {
        for ($k = 0; $k < count($a); $k++) {
            $id = $a[$k]['id'];
            $name = $a[$k]['name'];
            $route = $BASE_URL . $a[$k]['route'];
            //输出二级菜单项
            //格式：func_array['f54']=['OA消息','your url'];
            echo "func_array['f" . $id . "']=['" . $name . "','" . $route . "'];";
        }
    }
}

echo "</script>";








